﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Fca.Algorithms;
using Fca.DataStructures;

namespace grid
{
    /// <summary>
    /// Lattice data structure
    /// </summary>
    class Lattice
    {
        private List<LatticeNode> _Nodes;
        private HashSet<string> _AttributeNames;
        private HashSet<string> _ObjectNames;

        public List<LatticeNode> Nodes { get { return _Nodes; } }
        public HashSet<string> Attributes { get { return _AttributeNames; } }
        public HashSet<string> Objects { get { return _ObjectNames; } }

        /// <summary>
        /// Default constructor
        /// </summary>
        public Lattice()
        {
            _Nodes = new List<LatticeNode>();
            _AttributeNames = new HashSet<string>();
            _ObjectNames = new HashSet<string>();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="arrCells"></param>
        /// <param name="arrObjectNames"></param>
        /// <param name="arrAttributeNames"></param>
        public Lattice(bool[,] arrCells, string[] arrObjectNames, string[] arrAttributeNames) : this()
        {
            FormalContext fcContext = new FormalContext(arrCells, arrObjectNames, arrAttributeNames);
            FasterStabilityContextManager scm = new FasterStabilityContextManager(fcContext);
   
            foreach (string attrName in scm.Context.AttributeNames)
            {
                _AttributeNames.Add(attrName);
            }

            foreach (string objName in scm.Context.ObjectNames)
            {
                _ObjectNames.Add(objName);
            }

            foreach (Fca.DataStructures.LatticeNode lnNode in scm.Concepts)
            {
                _Nodes.Add(new LatticeNode(lnNode, scm.Context));
            }

            //Convert Fca.LatticeNode to new LatticeNode in lattice
            for (int i = 0; i < scm.Concepts.Count; i++)
            {
                Fca.DataStructures.LatticeNode node = scm.Concepts[i];
                for (int j = 0; j < scm.Concepts.Count; j++)
                {
                    for (int k = 0; k < node.Parents.Count; k++)
                    {
                        Fca.DataStructures.LatticeNode p = node.Parents[k];
                        if (scm.Concepts[j].CompareTo(p) == 0)
                        {
                            _Nodes[i].AddParent(_Nodes[j]);
                            _Nodes[j].AddChild(_Nodes[i]);
                        }
                    }
                }
            }

        }
    }
}
